import os
import sys

# 获取项目根目录的绝对路径
project_root = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..'))
sys.path.insert(0, project_root)

import subprocess

# 定义项目根目录的绝对路径
BASE_DIR = project_root

class RunScript:
    def __init__(self):
        pass

    @staticmethod
    def run_list_spider():
        # 切换到列表页爬虫根目录
        project_root = os.path.join(BASE_DIR, "service/list_service", "list_service")
        if os.path.exists(project_root):
            os.chdir(project_root)
            # 构建Scrapy命令
            command = "scrapy crawl list_crawl"
            # 执行命令并将其作为子进程运行
            process = subprocess.Popen(command, shell=True)
            process.wait()
        else:
            print(f"路径 {project_root} 不存在，请检查路径是否正确。")

    @staticmethod
    def run_detail_spider():
        # 切换到详情页爬虫根目录
        project_root = os.path.join(BASE_DIR, "service/detail_service", "detail_service")
        if os.path.exists(project_root):
            os.chdir(project_root)
            # 构建 Scrapy 命令
            command = "scrapy crawl detail_crawl"
            # 执行命令并将其作为子进程运行
            process = subprocess.Popen(command, shell=True)
            process.wait()
        else:
            print(f"路径 {project_root} 不存在，请检查路径是否正确。")


if __name__ == "__main__":

    # 运行 NewsListService
    RunScript.run_list_spider()
    # 运行 NewsDetailSpider
    RunScript.run_detail_spider()